/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bryanprogramming.zend.logic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
import javax.swing.JOptionPane;

/**
 *
 * @author charlesbryan
 */
public class Dictionary {
    
    private static HashSet<String> dictionary; 
    
    public static void load()  {
        try {
            dictionary = loadDictionary(4, 16);
        }
        catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "Could not load Dictionary!\nExiting.", "Error", JOptionPane.ERROR_MESSAGE);
            System.exit(9);
        }
    }
    
    public static void addTestWord(String testWord) {
        dictionary.add(testWord);
    }
    
    public static boolean isValidWord(String word) {
        return dictionary.contains(word);
    }
    
    private static HashSet<String> loadDictionary(int min, int max) throws FileNotFoundException {
        HashSet<String> dict = new HashSet<>();
        File file = new File("dictionary.txt");
        Scanner inFile = new Scanner(file);
        
        PrintWriter outFile = new PrintWriter("Small Dictionary.txt");
        
        while(inFile.hasNext()) {
            String word = inFile.next();
            if(word.length() >= min && word.length() <= max) {
                outFile.println(word);
                dict.add(word);
            }
        }
        outFile.close();
        return dict;
    } 
    
    
}
